<template>
  <div>
    <!-- 导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-button type="success">添加角色</el-button>
    </el-card>

    <!-- 表格区域 -->
    <el-table :data="rolesTreeList" style="width: 100%" border stripe>
      <!-- 扩展列 -->
      <el-table-column type="expand">
        <!-- 作用域插槽 -->
        <template slot-scope="{ row }">
          <el-row
            v-for="(item1, i1) in row.children"
            :key="item1.id"
            class="btbuttum vcenter"
            :class="{ bttop: i1 === 0 }"
          >
            <!-- 渲染第一级 -->
            <el-col :span="5">
              <el-tag @close="deleteRight(row, item1.id)" closable>
                {{ item1.authName }}
              </el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
            <!--渲染第二级和第三季  -->
            <el-col :span="19">
              <el-row
                v-for="(item2, i2) in item1.children"
                :key="item2.id"
                class="vcenter"
                :class="{ bttop: i2 !== 0 }"
              >
                <!-- 第二级 -->
                <el-col :span="6">
                  <el-tag
                    type="success"
                    closable
                    @close="deleteRight(row, item2.id)"
                    >{{ item2.authName }}</el-tag
                  >
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <!-- 第三级 -->
                <el-col :span="18">
                  <el-tag
                    v-for="item3 in item2.children"
                    :key="item3.id"
                    type="warning"
                    closable
                    @close="deleteRight(row, item3.id)"
                    >{{ item3.authName }}</el-tag
                  >
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="roleName" label="角色名称"> </el-table-column>
      <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="{ row }">
          <el-button type="primary">编辑</el-button>
          <el-button type="warning">删除</el-button>
          <el-button type="success" @click="showSetRightDialog(row)"
            >分配权限</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible.sync="dialogVisible" width="50%">
      <template>
        <!-- 树形控件 -->
        <el-tree
          :data="rolesList"
          :props="rolesProps"
          show-checkbox
          node-key="id"
          :default-expand-all="true"
          :default-checked-keys="rolesKeys"
        ></el-tree>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 所有角色列表数据
      rolesTreeList: [],
      dialogVisible: false,
      rolesList: [],
      rolesProps: {
        label: 'authName',
        children: 'children'
      },
      rolesKeys: []
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    // 获取所有劫色权限
    async showSetRightDialog (row) {
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        return this.$message({
          message: '获取角色失败!',
          type: 'error',
          showClose: true,
          duration: 3000
        })
      }
      // 获取成功,将数据赋值给权限列表
      this.rolesList = res.data
      this.getRolesdigui(row, this.rolesKeys)
      this.dialogVisible = true
    },
    // 定义递归函数获取三级权限的id
    getRolesdigui (item, arr) {
      // 如果这个节点没有children属性,那么就证明是3级权限,那么就将id传给这个数组
      if (!item.children) {
        return arr.push(item.id)
      }
      // 如果不是3级节点
      item.children.forEach(itemNode => {
        this.getRolesdigui(itemNode, arr)
      })
    },
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      console.log(res)
      this.rolesTreeList = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.btttop {
  border-top: solid 1px #eee;
}

.btBottom {
  border-bottom: solid 1px #eee;
}
</style>
